package be.pluyckx.batteryprogress.activity.fragment.info.history;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import be.pluyckx.batteryprogress.R;
import be.pluyckx.batteryprogress.activity.fragment.info.ItemDetailsFragment;
import be.pluyckx.batteryprogress.components.adapter.BatteryListAdapter;
import be.pluyckx.batteryprogress.data.history.BatteryHistory;

/**
 * Created by Philip on 21/08/13.
 *
 * A Fragment that displays the BatteryHistory as a list.
 */
public class ListHistoryFragment extends Fragment {
  private static final String TAG = "ListHistoryFragment";

  private ListView lvHistory;
  /**
   * The Adapter for the ListView
   */
  private BatteryListAdapter mAdapter;
  private BatteryHistory mBatteryHistory = BatteryHistory.getHistory();

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_list_history, container, false);

    lvHistory = (ListView) v.findViewById(R.id.lv_level_history);
    mAdapter = new BatteryListAdapter(getActivity());
    lvHistory.setAdapter(mAdapter);
    lvHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (i >= 0) {
          ItemDetailsFragment fragment = new ItemDetailsFragment((int) adapterView.getItemIdAtPosition(i));
          fragment.show(getActivity().getSupportFragmentManager(), "DetailedInfoFragment");
        }
      }
    });

    return v;
  }

  @Override
  public void onResume() {
    super.onResume();
  }

  @Override
  public void onPause() {

    super.onPause();
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();

    mAdapter.destroy();
  }
}
